"""
@author: wy
@file: download.py
@time: 2023/12/4 15:51
"""
import time

import requests

#获取文本
def download(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0"
    }
    try:#try内部放入可能出错的代码
        response = requests.get(url, headers=headers,verify=False)
        print(response)
        #获取状态码
        response.raise_for_status()
        #设置编码格式
        response.encoding = response.apparent_encoding

        time.sleep(3)
        # print(response)  # 返回对象的状态码code
        # print(response.text)  # 返回对象的文本内容
        # print(response.status_code)  # 放回对象的状态码code200
        # print(response.json())  # 放回对象的json格式的内容
        # print(type(response))  # 返回对象类型
        # print(response.reason)  # 返回响应信息
        return response.text

    except:
        print(url,"访问错误")
        return ""


#获取文本content
def download_img(url):
    try:#try内部放入可能出错的代码
        headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0"
        }
        response = requests.get(url, headers=headers,verify=False)
        #获取状态码
        response.raise_for_status()
        #设置编码格式
        response.encoding = response.apparent_encoding

        time.sleep(3)

        return response.content
    except:
        print(url,"访问错误")
        return b""


